package com.example.mdteachingsys.stu.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xscj.domain.Message;
import com.xscj.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;

public class ShowStudentMessageAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 3141028465645313809L;

    @Autowired
    @Qualifier("messageServiceImpl")
    private MessageService messageService;

    private List<Message> messageList;

    public List<Message> getMessageList() {
        return messageList;
    }

    public void setMessageList(List<Message> messageList) {
        this.messageList = messageList;
    }

    private int rowTotal; //总共有多少条学生记录
    private int pageSize = 10; //每一页可以显示多少条记录
    private int pageNow = 1; //当前是第几页
    private int pageTotal; //总共有多少页

    public int getPageSize() {
        return pageSize;
    }

    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getRowTotal() {
        return rowTotal;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    @Override
    public String execute() throws Exception {
        //rowTotal = stuService.getStuCount();
        ActionContext ctx = ActionContext.getContext();
        String userName = (String) ctx.getSession().get("userName");
        String userRole = (String) ctx.getSession().get("userRole");
        if (userName != null && userRole.equals("学生")) {
        rowTotal = messageService.getStudentMessageCount(userName);
            pageTotal = ((rowTotal % pageSize) == 0) ? (rowTotal / pageSize) : (rowTotal / pageSize + 1);
            if (pageNow <= 0)
                pageNow = 1;
            if (pageNow > pageTotal)
                pageNow = pageTotal;
            if (rowTotal > 0) {
                messageList = messageService.queryStudentMessageByPage(pageSize, pageNow, userName);
            }
        }
        return SUCCESS;
    }
}
